package com.chixing.day18_thread;


public class PublicResource {

    private int number = 0;
    //生产面包
    public synchronized void producer(){
        if (number == 20){
            try { //不生产 生产者线程进入等待队列 等待消费者消费
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else //如果
            number++;
        System.out.println("..生产者生产了一个面包: " + number);
        try {
            Thread.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //让消费者消费
        this.notify();
    }

    //消费面包
    public synchronized void consume(){
        if (number == 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else {
            number--;
            System.out.println("--消费者生产了一个面包: " + number);
            //让生产者生产
            this.notify();
        }

    }
}
